package com.qxsx.midi.activity.personal;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.home.BuyListActivity;
import com.qxsx.midi.activity.me.MeKejiantActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;

/**
 * Created by LYJ on 2015/9/8.
 */
public class PersonalDetailsNewActivity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private RelativeLayout rl_mess, rl_name, rl_email, rl_position, rl_password, rl_phonenum;
    private LinearLayout ll_back;
    TextView tv_id;
    CircleImageView img_head;
    Intent intent = new Intent();
    private EditText ed_name, ed_idcard, ed_email;
    String userid;
    String midi_id, nickname, real_name, eamil, phone, id_card, nationality, birthday, people, head_img;
    int sex;
    Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_datails_n);
        setTitle("个人信息");
        setNavBtn(R.mipmap.back, "", 0, "保存");
        rl_mess = (RelativeLayout) findViewById(R.id.detail_mess);
        rl_password = (RelativeLayout) findViewById(R.id.detail_password);
        rl_phonenum = (RelativeLayout) findViewById(R.id.detail_phonenum);
        // tv_name = (TextView) findViewById(R.id.tv_name);
        tv_id = (TextView) findViewById(R.id.tv_id);
        img_head = (CircleImageView) findViewById(R.id.detail_head);
        ed_name = (EditText) findViewById(R.id.ed_name);
        ed_email = (EditText) findViewById(R.id.ed_email);
        ed_idcard = (EditText) findViewById(R.id.ed_identity);

        rl_mess.setOnClickListener(this);
        rl_password.setOnClickListener(this);
        rl_phonenum.setOnClickListener(this);

        userid = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        // OnAccess(userid);

    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

            case RIGHT:
                if (checkyy()) {


                    LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
                    View view = layout.inflate(R.layout.dialog_buy_finish, null);
                    dialog = new AlertDialog.Builder(PersonalDetailsNewActivity.this).create();
                    TextView tv_zhifu = (TextView)view.findViewById(R.id.tv_zhifu);
                    tv_zhifu.setText("是否保存？");
                    Button btn_see = (Button) view.findViewById(R.id.btn_see);
                    btn_see.setText("是");
                    Button btn_clance = (Button) view.findViewById(R.id.btn_cancel);
                    btn_clance.setText("否");
                    btn_clance.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            dialog.dismiss();

                        }
                    });
                    btn_see.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            newa();
                            dialog.dismiss();
                        }
                    });
                    DisplayMetrics dm = new DisplayMetrics();
                    PersonalDetailsNewActivity.this.getWindowManager().getDefaultDisplay().getMetrics(dm);
                    int x = dm.widthPixels;// 屏幕的宽
                    int y = dm.heightPixels;// 屏幕的高
                    dialog = new Dialog(PersonalDetailsNewActivity.this, R.style.dialog_finish);
                    dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));
                    dialog.show();
                    dialog.setContentView(view);
                    dialog.setCancelable(false);


                }
                break;

        }
    }


    private boolean checkyy() {
        if (TextUtils.isEmpty(ed_name.getText().toString())) {
            ShowToast("请输入您的姓名");
            return false;
        }
        if (TextUtils.isEmpty(ed_idcard.getText().toString())) {
            ShowToast("请输入您的身份证");
            return false;
        }
        if (TextUtils.isEmpty(ed_email.getText().toString())) {
            ShowToast("请输入您的邮箱");
            return false;
        }
        try {
            if (!Tools.IDCardValidate(ed_idcard.getText().toString()).equals("")){
                ShowToast("请输入正确的身份证号");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (!Tools.isEmail(ed_email.getText().toString())){
            ShowToast("请输入正确的邮箱");
            return false;
        }

        return true;
    }

    /**
     * 更新
     */
    private void newa() {
        customProgress = CustomProgress.show(PersonalDetailsNewActivity.this, "正在保存....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");

        try {
            object.put("user_id", user_id);
            object.put("real_name", ed_name.getText().toString());
            object.put("id_card", ed_idcard.getText().toString());
            object.put("eamil", ed_email.getText().toString());
            object.put("sex", sex);
            object.put("nationality", nationality);
            object.put("people", people);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (code.toString().equals("200")) {
                    ShowToast(message.toString());
                    customProgress.dismiss();
                }
            }
        });

    }

    void OnAccess(String ID) {
        if (ID == "") {
            Log.e("id为空", ID);
            return;
        }
        customProgress = CustomProgress.show(PersonalDetailsNewActivity.this, "正在加载中....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("user_id", ID);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("109", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        if (!code.toString().equals("200")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                            try {
                                JSONObject jsonObject = array.getJSONObject(0);
                                midi_id = jsonObject.getString("midi_id");
                                nickname = jsonObject.getString("nickname");
                                real_name = jsonObject.getString("real_name");
                                eamil = jsonObject.getString("eamil");
                                phone = jsonObject.getString("phone");
                                id_card = jsonObject.getString("id_card");
                                nationality = jsonObject.getString("nationality");
                                birthday = jsonObject.getString("birthday");
                                people = jsonObject.getString("people");
                                sex = jsonObject.getInt("sex");
                                head_img = jsonObject.getString("head_img");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            tv_id.setText(midi_id);
                            ed_name.setText(real_name);
                            ed_idcard.setText(id_card);
                            ed_email.setText(eamil);
                            if (!head_img.equals("") && head_img != null && !head_img.isEmpty()) {
                                MyNet.setNetImage(AppContext.getContext(), img_head, head_img);

                            }
                        }
                        customProgress.dismiss();
                    }
                }
        );
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_mess:
                //完善信息
                intent.setClass(AppContext.getContext(), PersonalImproveNewActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_password:
                //修改密码
                intent.setClass(AppContext.getContext(), PersonalEditPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.detail_phonenum:
                //修改手机号码
                intent.setClass(AppContext.getContext(), PersonalEditPhoneNumActivity.class);
                startActivity(intent);
                break;

        }
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    // 像素转 dpi
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @Override
    protected void onResume() {
        super.onResume();
        OnAccess(userid);
    }

}
